package com.deep.deepenterpise.common;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

public class DrGson {
    private static final Gson gson;

    static {
        GsonBuilder gsonbuilder = new GsonBuilder();
        gson = gsonbuilder.create();
    }

    private DrGson() {
    }


    /**
     * @return java.lang.String
     * @Auth xyz
     * @Desc //TODO 对象转json
     * @Time 13:31 2024/5/13
     * @Param [object]
     */
    public static String toJSONString(Object object) {
        return gson.toJson(object);
    }

    /**
     * @return T
     * @Auth xyz
     * @Desc //TODO json转对象
     * @Time 13:48 2024/5/13
     * @Param [json, cls]
     */
    public static <T> T parseObject(String json, Class<T> clazz) {
        return gson.fromJson(json, clazz);
    }

    /**
     * @return java.util.Map<java.lang.String, T>
     * @Auth xyz
     * @Desc //TODO json转map<String,T>
     * @Time 09:09 2024/5/14
     * @Param [json]
     */
    public static <T> Map<String, T> parseMap(String json) {
        Map<String, T> map = null;
        if (gson != null) {
            map = gson.fromJson(json, new TypeToken<Map<String, T>>() {
            }.getType());
        }
        return map;
    }

    /**
     * @return java.util.List<java.util.Map < java.lang.String, T>>
     * @Auth xyz
     * @Desc //TODO json转list<map<String,T>>
     * @Time 09:55 2024/5/14
     * @Param [json]
     */
    public static <T> List<Map<String, T>> parseListMap(String json) {
        List<Map<String, T>> list = null;
        if (gson != null) {
            list = gson.fromJson(json, new TypeToken<List<Map<String, T>>>() {
            }.getType());
        }
        return list;
    }

    /**
     * @return java.util.List<T>
     * @Auth xyz
     * @Desc //TODO json转list<T>
     * @Time 13:48 2024/5/13
     * @Param [json, cls]
     */
    public static <T> List<T> parseArray(String json) {
        return gson.fromJson(json, new TypeToken<List<T>>() {
        }.getType());
    }

    /**
     * @return java.util.List<T>
     * @Auth xyz
     * @Desc //TODO json转list<T>
     * @Time 13:48 2024/5/13
     * @Param [json, cls]
     */
    public static <T> List<T> parseArray(String json, Class<T> clazz) {
        Type listType = TypeToken.getParameterized(List.class, clazz).getType();
        return gson.fromJson(json, listType);
    }


    /**
     * @return boolean
     * @Auth xyz
     * @Desc //TODO 判断是否是JSon
     * @Time 13:50 2024/5/13
     * @Param [jsonStr]
     */
    public static boolean validate(String jsonStr) {
        JsonElement jsonElement;
        try {
            jsonElement = JsonParser.parseString(jsonStr);
        } catch (Exception e) {
            return false;
        }
        if (jsonElement == null) {
            return false;
        }
        return jsonElement.isJsonObject();
    }
}
